<template>
  <div class='quickReply'>
    <el-dialog title="快速回复" :visible.sync="isShow" center :before-close="handleClose">
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in opinions"
          :key="index"
          :timestamp="item.created_at"
        >
          <el-card class="messagebox">
            <Tooltip content="点我回复" class="fr" >
              <i class="el-icon-chat-dot-round leaveWord ml5" @click="showTextarea(item, index)"></i>
            </Tooltip>
            <!-- <div class="item">
              <span class="item_time">{{item.created_at}}</span>
              <span class="item_user">审核员</span>
              <div class="item_message messageBackground_l">
                <span>{{item.review_cn}}</span>
              </div>
              <div class="deduction_message messageBackground_l" v-if="item.deduction_reason != ''">
                <div>
                  <span>扣款原因：</span>
                  <span class="ml10 font_danger">{{item.deduction_reason}}</span>
                </div>
                <div>
                  <span>扣款金额：</span>
                  <span class="ml10 font_danger">{{item.money_cn}}</span>
                </div>
              </div>
            </div> -->
            <div class="agent_item clearfloat" v-if="item.role_id == '2'">
              <span class="item_user fr">代理人</span>
              <span class="item_time fr">{{item.created_at}}</span>
              <div class="item_message messageBackground_r fr">
                <span>{{item.review_cn}}</span>
              </div>
            </div>
            <div v-else class="item">
              <span class="item_time">{{item.created_at}}</span>
              <span class="item_user">审核员</span>
              <div class="item_message messageBackground_l">
                <span>{{item.review_cn}}</span>
              </div>
              <div class="deduction_message messageBackground_l" v-if="item.deduction_reason != ''">
                <div>
                  <span>扣款原因：</span>
                  <span class="ml10 font_danger">{{item.deduction_reason}}</span>
                </div>
                <div>
                  <span>扣款金额：</span>
                  <span class="ml10 font_danger">{{item.money_cn}}</span>
                </div>
              </div>
            </div>
            <div v-for="(agent, index2) in item.agent_message" :key="index2">
              <div class="agent_item clearfloat" v-if="agent.role_id == '2'">
                <span class="item_user fr">代理人</span>
                <span class="item_time fr">{{agent.created_at}}</span>
                <div class="item_message messageBackground_r fr">
                  <span>{{agent.review_cn}}</span>
                </div>
              </div>
              <div v-else class="item">
                <span class="item_time">{{agent.created_at}}</span>
                <span class="item_user">审核员</span>
                <div class="item_message messageBackground_l">
                  <span>{{agent.review_cn}}</span>
                </div>
              </div>
            </div>
            <div v-if="item.show_input" class="clearfloat mt20">
              <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="replyMessage"></el-input>
              <el-button class="fr mt10" type="primary" @click="submit(item)">发送</el-button>
              <!-- <el-button class="fr mt10 mr10" @click="resets ">重置</el-button> -->
            </div>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      opinions: [],
      replyMessage: "",
      showInput: true,
    };
  },
  watch: {
    opinions: {
      handler: function(val) {
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    show(data) {
      this.replyMessage = ''
      this.isShow = true;
      this.opinions = data.opinions;
      for (let i = 0; i < this.opinions.length; i++) {
        this.opinions[0].show_input = true;
      }
    },
    submit(item) {
      if (this.replyMessage == "") {
        this.$message({
          message: "请输入消息",
          type: "warning"
        });
      } else {
        this.submitPost(item)
      }
    },
    showTextarea(item, index) {
      for(let i = 0; i < this.opinions.length; i++) {
        this.opinions[i].show_input = false;
        if (i == index) {
          this.opinions[i].show_input = true
        }
      }
    },
    submitPost(data) {
      let params = {
        opinion_id: data.id,
        review: this.replyMessage,
        type: 'auditor'
      }
      this.$axios
        .post(`/api/order/${data.order_id}/agent_message`, params)
        .then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "回复成功！"
            });
            this.isShow = false;
            this.$emit("refresh");
          } else {
            this.$message({
              type: "error",
              message: res.data.message
            });
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    handleClose() {
      this.isShow = false;
      this.replyMessage = "";
    }
  }
};
</script>

<style lang='less' type='text/css' scope>
.quickReply {
  .el-dialog {
    width: 900px;
    max-height: 800px;
    overflow-y: auto;
    margin-top: 5vh !important;
  }
  .messagebox {
    width: 100%;
    background: #eef1f3;
  }

  .messageBackground_l {
    background: #fff;
  }
  .messageBackground_r {
    background: #c9e7ff;
  }
  .item {
    width: 100%;
    margin-bottom: 10px;
    .item_user {
      display: inline-block;
      width: 60px;
      font-size: 16px;
      font-weight: bold;
    }
    .item_message {
      max-width: calc(100% - 170px);
      display: inline-block;
      padding: 10px 15px;
      box-sizing: border-box;
      margin-left: 15px;
      border-radius: 5px;
      vertical-align: top;
      // margin-right: 90px;
    }
    .item_time {
      font-size: 12px;
      display: block;
      padding-left: 80px;
      margin-bottom: 5px;
      color: #ccc;
    }
    .deduction_message {
      margin: 20px 0 0 80px;
      max-width: calc(100% - 170px);
      padding: 10px 15px;
      border-radius: 5px;
    }
  }
  .agent_item {
    width: 100%;
    margin-bottom: 10px;
    position: relative;
    box-sizing: border-box;
    padding-top: 20px;
    .item_user {
      display: inline-block;
      width: 60px;
      font-size: 16px;
      font-weight: bold;
    }
    .item_time {
      position: absolute;
      right: 95px;
      top: 0px;
      font-size: 12px;
      display: block;
      // padding-right: 80px;
      margin-bottom: 5px;
      color: #ccc;
    }
    .item_message {
      max-width: calc(100% - 170px);
      display: inline-block;
      padding: 10px 15px;
      box-sizing: border-box;
      margin-right: 30px;
      border-radius: 5px;
      vertical-align: top;
      // margin-left: 80px;
    }
  }
  .ivu-tooltip {
    position: absolute;
    right: 20px;
    top: 10px;
    z-index: 2000;
  }
}

</style>
